/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.eclipse.emf.ecore.provider;


import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EGenericType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.ETypeParameter;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.Parameter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;

import org.eclipse.emf.common.util.ResourceLocator;

import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.impl.EDataTypeImpl;
import org.eclipse.emf.ecore.impl.EPackageImpl;
import org.eclipse.emf.ecore.impl.EcoreFactoryImpl;
import org.eclipse.emf.ecore.impl.EcorePackageImpl;
import org.eclipse.emf.ecore.provider.EModelElementItemProvider.ItemPropertyDescriptorWithUniqueChoiceOfValueLabels;

import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.eclipse.emf.edit.provider.ViewerNotification;
import org.eclipse.swt.widgets.Shell;

import edu.sysu.emf.extendedEcore.ui.AddUserDefinedTypeDialog;

/**
 * This is the item provider adapter for a {@link ecore.Parameter} object.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public class ParameterItemProvider
	extends ItemProviderAdapter
	implements
		IEditingDomainItemProvider,
		IStructuredItemContentProvider,
		ITreeItemContentProvider,
		IItemLabelProvider,
		IItemPropertySource {
	/**
	 * This constructs an instance from a factory and a notifier.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ParameterItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	
	}

	/**
	 * This returns the property descriptors for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public List getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addTypePropertyDescriptor(object);
			addNamePropertyDescriptor(object);
			
		}
		return itemPropertyDescriptors;
	}

	/**
	 * This adds a property descriptor for the Type feature.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	/*protected void addTypePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_parameter_type_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_parameter_type_feature", "_UI_parameter_type"),
				 EcorePackage.Literals.PARAMETER__TYPE,
				 true,
				 false,
				 true,
				 null,
				 null,
				 null));
	}
*/
	
	//@chenxp
	//testing adding data types
	
	protected void addTypePropertyDescriptor(Object object)
	  {
	    itemPropertyDescriptors.add
	      (new ItemPropertyDescriptorWithUniqueChoiceOfValueLabels
	        (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
	         getResourceLocator(),
	         getString("_UI_parameter_type_feature"),
	         getString("_UI_parameter_type_description"),
	         EcorePackage.Literals.PARAMETER__TYPE,
	         true,
	         false,
	         true,
	         null,
	         null,
	         null)
	       {
	         @Override
	         public Collection<?> getChoiceOfValues(Object object)
	         {
	           // Filter out types that aren't permitted.
	           //
	           Collection<Object> result = new ArrayList<Object>(super.getChoiceOfValues(object));
	           // System.out.println(object);
	           if (!(object instanceof EReference))
	           {
	             for (Object classifier :  EcorePackage.eINSTANCE.getEClassifiers())
	             {
	               if (!result.contains(classifier))
	               {
	                 result.add(classifier);
	                
	               }
	             }
	           }
	           if (!result.contains(EcorePackage.Literals.EOBJECT))
	           {
	             result.add(EcorePackage.Literals.EOBJECT);
	           }
	           if (object instanceof Parameter)
	           {
	             for (Iterator<Object> i = result.iterator(); i.hasNext(); )
	             {
	               if (i.next() instanceof EClass)
	               {
	                 i.remove();
	               }
	             }
	           }
	           if (object instanceof EAttribute)
	           {
	             for (Iterator<Object> i = result.iterator(); i.hasNext(); )
	             {
	               if (i.next() instanceof EClass)
	               {
	                 i.remove();
	               }
	             }
	           }
	           else if (object instanceof EReference)
	           {
	             for (Iterator<Object> i = result.iterator(); i.hasNext(); )
	             {
	               if (i.next() instanceof EDataType)
	               {
	                 i.remove();
	               }
	             }
	           }
	           
	           // Let them choose type parameters as well.
	           //
	           for (EObject eObject = (EObject)object; eObject != null; eObject = eObject.eContainer())
	           {
	             if (eObject instanceof EClassifier)
	             {
	               result.addAll(((EClassifier)eObject).getETypeParameters());
	             }
	             else if (eObject instanceof EOperation)
	             {
	               result.addAll(((EOperation)eObject).getETypeParameters());
	             }
	           }

	           uniqueNameMap = computeUniqueLabels(object, result);

	           System.out.println(result.size());
	         // this.getPropertyValue(object);
	           return result;
	         }
	        
	    	         
	        @Override
	         public void setPropertyValue(Object object, Object value)
	         {
	        	/*
	           EditingDomain editingDomain = getEditingDomain(object);
	           if (editingDomain == null)
	           {
	             super.setPropertyValue(object, value);
	           }
	           else 
	           {
	             
	             EGenericType eGenericType = null;
	             if (value instanceof EClassifier)
	             {
	               EClassifier eClassifier = (EClassifier)value;
	               eGenericType = EcoreFactory.eINSTANCE.createEGenericType();
	               eGenericType.setEClassifier(eClassifier);
	               for (int i = 0, size = eClassifier.getETypeParameters().size(); i < size; ++i)
	               {
	                 eGenericType.getETypeArguments().add(EcoreFactory.eINSTANCE.createEGenericType());
	               }
	             }
	             else if (value instanceof ETypeParameter)
	             {
	               eGenericType = EcoreFactory.eINSTANCE.createEGenericType();
	               eGenericType.setETypeParameter((ETypeParameter)value);
	             }
	             editingDomain.getCommandStack().execute
	               (SetCommand.create(editingDomain, object, EcorePackage.Literals.ETYPED_ELEMENT__EGENERIC_TYPE, eGenericType));
	             }
	           */
	               
		           
	        	   System.out.println("testing data type" + value);
	        	   
	        	   if(value instanceof EDataType && ((EDataType) value).getName()!= null &&((EDataType) value).getName().equals("EUserDefinedType")){
	        	//	   System.out.println("testing data type" + value);	        		   
	        		   EDataTypeImpl dataType =  (EDataTypeImpl) EcoreFactoryImpl.eINSTANCE.createEDataType();
	        		   AddUserDefinedTypeDialog dialog = new AddUserDefinedTypeDialog(new Shell());
	        	       dialog.open(); 
	        	       if(dialog.getReturnCode() == dialog.OK){
	        	    	   dataType.setInstanceClassName(dialog.getInstanceClassName());	        	
	        	    	   dataType.setName(dialog.getName());
	        	    	   dataType.setSerializable(true);
	        	    	   dataType.setClassifierID(-2);	        	    	 
	        	    	}   
	        	        
	        	       this.ecorePackage.getEClassifiers().add(dataType);
	        	       super.setPropertyValue(object, dataType);
	        	   }
	        	   else
	        	     super.setPropertyValue(object, value); 
	         }
	       });
	  }

	/**
	 * This adds a property descriptor for the Name feature.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void addNamePropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_parameter_name_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_parameter_name_feature", "_UI_parameter_type"),
				 EcorePackage.Literals.PARAMETER__NAME,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This specifies how to implement {@link #getChildren} and is used to deduce an appropriate feature for an
	 * {@link org.eclipse.emf.edit.command.AddCommand}, {@link org.eclipse.emf.edit.command.RemoveCommand} or
	 * {@link org.eclipse.emf.edit.command.MoveCommand} in {@link #createCommand}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Collection getChildrenFeatures(Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures.add(EcorePackage.Literals.PARAMETER__MAPPING_INOFO);
		}
		return childrenFeatures;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected EStructuralFeature getChildFeature(Object object, Object child) {
		// Check the type of the specified child object and return the proper feature to use for
		// adding (see {@link AddCommand}) it as a child.

		return super.getChildFeature(object, child);
	}

	/**
	 * This returns parameter.gif.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/parameter"));
	}

	/**
	 * This returns the label text for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getText(Object object) {
		String label = ((Parameter)object).getName();
		return label == null || label.length() == 0 ?
			getString("_UI_parameter_type") :
			getString("_UI_parameter_type") + " " + label;
	}

	/**
	 * This handles model notifications by calling {@link #updateChildren} to update any cached
	 * children and by creating a viewer notification, which it passes to {@link #fireNotifyChanged}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(Parameter.class)) {
			case EcorePackage.PARAMETER__NAME:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
			case EcorePackage.PARAMETER__MAPPING_INOFO:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
				return;
		}
		super.notifyChanged(notification);
	}

	/**
	 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
	 * that can be created under this object.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void collectNewChildDescriptors(Collection newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);

		newChildDescriptors.add
			(createChildParameter
				(EcorePackage.Literals.PARAMETER__MAPPING_INOFO,
				 EcoreFactory.eINSTANCE.createmapping()));
	}

	/**
	 * Return the resource locator for this item provider's resources.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ResourceLocator getResourceLocator() {
		return EcoreEditPlugin.INSTANCE;
	}

}
